/**
 *
 */

layui.use('table', function () {
    var table = layui.table;

    // 第一个实例
    table.render({
        elem: '#UserMessage',
        height: 500,
        cellMinWidth: 100,
        url: '/manager/userEssay.do', // 数据接口
        page: true, // 开启分页
        id:'allEssay',
        cols: [[ // 表头
            {
                field: 'essayId',
                title: '文章编号',
                sort: true,
                align: 'center'
            }, {
                field: 'userName',
                title: '发布者名称',
                align: 'center',
                templet: function (value) {
                    return '<span>' + value.user.userName + '</span>'
                }
            }, {
                field: 'title',
                title: '文章标题',
                align: 'center'
            }, {
                field: 'label',
                title: '文章标签',
                align: 'center'
            }, {
                field: 'category',
                title: '文章类别',
                align: 'center',
                sort:true,
                templet: function (value) {
                    if ('downLoadEssay' == value.category) {
                        return '下载帖'
                    } else {
                        return '普通帖'
                    }
                }
            }, {
                field: 'publishDate',
                title: '文章发布日期',
                sort: true,
                align: 'center'
            }, {
                field: 'auditing',
                title: '审核状态',
                align: 'center',
                templet: function (value) {
                    if (1 == value.auditing) {
                        return '已审核'
                    } else {
                        return '待审核'
                    }
                }

            }, {
                fixed: 'right',
                title: '操作',
                align: 'center',
                toolbar: '#barDemo',
                width: 150
            }]]
    });

    //监听行工具事件
    table.on('tool(all)', function (obj) {
        var data = obj.data;
        var essayId = data.essayId;
        if (obj.event === 'del') {
            layer.confirm('确定删除文章吗', function (index) {
                layer.prompt({
                    formType: 2,
                    title: '请输入删除原因以及提醒',
                    area: ['500px', '200px'] //自定义文本域宽高
                }, function (value, index, elem) {
                    if (value != null && value != '') {
                        $.ajax({
                            type: 'POST',
                            url: '/manager/DeleteEssay.do',
                            data: {
                                essayId: essayId,
                                content: value
                            },
                            success: function (data) {
                                if (data == "success") {
                                    layer.msg("操作成功", {
                                        icon: 1,
                                        time: 500
                                    }, function () {
                                       /* window.location.href = "/manager/messageredirect.do"*/
                                        obj.del();
                                        layer.close(index);
                                    });
                                } else if (data == "false") {
                                    layer.msg("服务器好像出错了，请稍后重试", {
                                        icon: 2,
                                        time: 500
                                    }, function () {
                                        window.location.href = "/manager/messageredirect.do"
                                    });
                                }
                            }
                        });
                    } else {
                        layer.msg("请输入删除原因！！", {
                            icon: 2,
                            time: 500
                        });
                    }
                });
            });
        } else if (obj.event === 'auditing') {
            var essayId = data.essayId;
            parent.frames["main"].window.location.href = "/essay/auditingEssay.do?essayId=" + essayId;
        }
    });

    //表格重载数据/根据ID查找
    $("#searchId").click(function () {
        var essayId=$("#essayId").val();
        table.reload('allEssay', {
            url: '/manager/userEssaySelectAll.do',
            where: {
                essayId:essayId
            }
        });
    });

    //表格重载数据/根据标题类别查找
    $("#searchTitle").click(function () {
        var Title=$("#Title").val();
        table.reload('allEssay', {
            url: '/manager/userEssaySelectAll.do',
            where: {
                essayId:'',
                title:Title
            }
        });
    });
});